﻿$(document).on('pageshow', '#cet', function(){
	$('#submitBtn').unbind();
	var cet_name = getCookie('cet_name') ? unescape(getCookie('cet_name')) : null;
	var cet_num = getCookie('cet_num');
	if(cet_name) $('#name').val(cet_name);
	if(cet_num) $('#number').val(cet_num);
	$('#submitBtn').on('tap', function(){
		var cet_name = $('#name').val();
		var cet_num = $('#number').val();
		if(!/^[\u4E00-\uFA29]|[\uE7C7-\uE7F3]+$/.test(cet_name)){
			$('#warnText').text('请输入正确的姓名');
			$('#warningBtn').click();
		}
		else if(!/\d{15}/.test(cet_num)){
			$('#warnText').text('请输入15位准考证号');
			$('#warningBtn').click();
		}else{	
			setCookie('cet_name', cet_name);
			setCookie('cet_num', cet_num);
			$.mobile.changePage('#result',{
				transition:'slide'
			});
		}
	});
});

$(document).on('pageshow', '#result', function(){
	var cet_name = getCookie('cet_name') ? unescape(getCookie('cet_name')) : null;
	var cet_num = getCookie('cet_num');
	if(!/^[\u4E00-\uFA29]|[\uE7C7-\uE7F3]+$/.test(cet_name) || !/\d{15}/.test(cet_num)){
		$.mobile.changePage('#cet',{
			transition:'slide'
		});
	}else{
		showLoader();
		$.ajax({
			url: './php/get_cet.php',
			type: 'post',
			dataType: 'json',
			data: '&name='+cet_name+'&number='+cet_num,
			success:
				function(data){
					hideLoader();
					if(data.error){
						$('#warnText').text(data.error);
						$('#warningBtn').click();
					}else{
						$('#school').text(data.school);
						$('#resname').text(data.name);
						$('#type').text(data.type);
						$('#time').text(data.time);
						$('#total').text(data.total);
						$('#listen').text(data.listen);
						$('#read').text(data.read);
						$('#write').text(data.write);
						$('#resultTable').table("refresh");
					}
				}
		});
	}
});